package com.aplink.generic.xmlparser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.aplink.generic.constant.ConfigParameter;
import com.aplink.generic.pojo.RecordInfo;

public class RecordInfoParser {

    public static ArrayList<RecordInfo> parse(final Document doc) {

        final ArrayList<RecordInfo> recordInfoList = new ArrayList<RecordInfo>();
        final NodeList nList = doc
                .getElementsByTagName(ConfigParameter.TABLE_STRUCTURE);

        for (int temp = 0; temp < nList.getLength(); temp++) {

            final RecordInfo recordInfo = new RecordInfo();
            final Node nNode = nList.item(temp);
            String tableId = "";
            // set tableInfo ID
            if (nNode.hasAttributes()) {
                final Attr attr = (Attr) nNode.getAttributes().getNamedItem(
                        ConfigParameter.TABLE_ID);
                if (attr != null) {
                    tableId = attr.getNodeValue();
                    recordInfo.setmTableName(tableId);
                }
            }

            final ArrayList<Hashtable<String, String>> mRecords = new ArrayList<Hashtable<String, String>>();

            // set obj_prop tags
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                final Element element = (Element) nNode;

                // get Obj_prop tags of this node
                final NodeList recordNodes = element
                        .getElementsByTagName(ConfigParameter.TABLE_DATA_RECORD);

                for (int recordIndex = 0; recordIndex < recordNodes.getLength(); recordIndex++) {

                    final Hashtable<String, String> record = new Hashtable<String, String>();
                    final Node nRecordNode = recordNodes.item(recordIndex);
                    final Element elementRecordNode = (Element) nRecordNode;
                    final NodeList nodes = elementRecordNode
                            .getElementsByTagName(ConfigParameter.TABLE_DATA_ATTRIBUTE);

                    for (int attributeIndex = 0; attributeIndex < nodes
                            .getLength(); attributeIndex++) {
                        final Node nAtrributeNode = nodes.item(attributeIndex);

                        String atributeId = "";
                        String atributeValue = "";

                        if (nAtrributeNode.hasAttributes()) {
                            Attr attr = (Attr) nAtrributeNode.getAttributes()
                                    .getNamedItem(
                                            ConfigParameter.TABLE_ATTRIBUTE_ID);
                            if (attr != null) {
                                atributeId = attr.getNodeValue();
                            }
                            attr = (Attr) nAtrributeNode
                                    .getAttributes()
                                    .getNamedItem(
                                            ConfigParameter.TABLE_ATTRIBUTE_VALUE);
                            if (attr != null) {
                                atributeValue = attr.getNodeValue();
                            }

                            if (atributeId != "") {
                                record.put(atributeId, atributeValue);
                            }
                        }
                    }
                    // add to main Records of this table
                    mRecords.add(record);

                }
            }

            // set recordInfo for this table
            recordInfo.setmRecordList(mRecords);
            recordInfoList.add(recordInfo);

        }
        return recordInfoList;
    }

    public static ArrayList<RecordInfo> parseRecordsTables(final int xmlFileId) {
        try {
            final Document document = XMLPaserUtil.openDocumentFrom(xmlFileId);
            return RecordInfoParser.parse(document);
        } catch (final ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (final SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (final IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
